/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.database;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author himanasser
 */
public class Manager {
	private static List<PooledConnection> connectionPool = new ArrayList<PooledConnection>();

	public static synchronized Connection getConnection() {
		for (PooledConnection pconn : connectionPool) {
			Connection conn = pconn.getConnection();
			if (conn != null)
				return conn;
		}

		PooledConnection pconn = new PooledConnection(
				ConnectionFactory.createConnection());
		connectionPool.add(pconn);
		return pconn.getConnection();
	}

	public static synchronized void closeConnection(Connection conn) {
		for (PooledConnection pconn : connectionPool) {
			if (pconn._getUnderlyingConn() == conn) {
				pconn.releaseConnection();
				return;
			}
		}
		System.err.println("Connection " + conn + " Cannot be release!");
	}

	static {
		for (int i = 0; i < 10; i++) {
			connectionPool.add(new PooledConnection(ConnectionFactory
					.createConnection()));
		}
	}

}
